﻿using System.Text;
using Newtonsoft.Json;
using PkwHCFramework.Communications;
using SqlSugar;

namespace PkwHCFramework.Core
{
    public class DataMonitorService
    {
        public List<DeviceLink> LinkModels = new List<DeviceLink>();

        public List<RegisterPoint> AllPoints = new List<RegisterPoint>();

        public DataMonitorService()
        {
            Init();
        }

        void Init()
        {
            //加载链路和点位，并启动监控
            var path = AppDomain.CurrentDomain.BaseDirectory + "Config";
            var str = File.ReadAllText(path + "\\LinkConfig.json");
            LinkModels = JsonConvert.DeserializeObject<List<DeviceLink>>(str);
            var lines = File.ReadAllLines($"{path}\\AllPoint.csv", Encoding.UTF8).ToList();
            lines.RemoveAt(0);
            List<RegisterPoint> points = new List<RegisterPoint>();
            foreach (var line in lines)
            {
                var values = line.Split(',');
                var point = new RegisterPoint()
                {
                    UID = values[0],
                    Name = values[1],
                    Type = Type.GetType(values[2]),
                    RegisterType = values[3],
                    Address = int.Parse(values[4]),
                    Length = int.Parse(values[5]),
                    DeviceId = values[6],
                    Tag = values[7],
                };
                LinkModels.FirstOrDefault(x => x.UID == point.DeviceId)?.Points.Add(point);
                AllPoints.Add(point);
            }

            foreach (var link in LinkModels)
            {
                ModbusTcp modbusTcp = new ModbusTcp(link);
                modbusTcp.ValueUpdated += ModbusTcp_ValueUpdated;
                modbusTcp.DoMonitor();
            }
        }

        #region 监控读写

        /// <summary>
        /// 值变化触发
        /// </summary>
        /// <param name="point"></param>
        /// <param name="value"></param>
        private void ModbusTcp_ValueUpdated(RegisterPoint point, object value)
        {
            //逻辑处理
        }

        /// <summary>
        /// 批量写入
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="points"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Writes(string uid, List<RegisterPoint> points)
        {
            var link = LinkModels.FirstOrDefault(x => x.UID == uid);
            if (link == null)
            {
                throw new ArgumentNullException("未找到");
            }
            foreach (var point in points)
            {
                link.Comm.Write(point);
            }
        }

        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="point"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Write(string deviceid, RegisterPoint point)
        {
            var link = LinkModels.FirstOrDefault(x => x.UID == deviceid);
            if (link == null)
            {
                throw new ArgumentNullException("未找到");
            }

            link.Comm.Write(point);
        }
        #endregion
    }
}
